package communication

import (
	"gitee.com/thingple/saturn/internal/rest"
)

type RestJob struct {
	Api     string            `json:"api,omitempty"`
	Schema  string            `json:"schema,omitempty"`
	Method  string            `json:"method,omitempty"`
	Host    string            `json:"host,omitempty"`
	Path    string            `json:"path,omitempty"`
	Query   map[string]string `json:"query,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
	Cookies map[string]string `json:"cookies,omitempty"`
}

func (rj *RestJob) Fetch(respPtr any) (err error) {
	r := rest.NewRequest(rj.Host, rj.Schema == "https")
	r = r.Path(rj.Path)

	if len(rj.Headers) > 0 {
		for k, v := range rj.Headers {
			r.Header(k, v)
		}
	}
	if len(rj.Query) > 0 {
		m := rj.Query
		for k, v := range m {
			r.Query(k, v)
		}
	}
	r.Response(respPtr)
	err = r.Do(rj.Method)
	return
}
